<template>
   <div class="tags_wrap" v-if="showTags">
        <!-- <el-tabs type="border-card">
            <el-tab-pane
            v-for="(item,index) in tagsList"
            :key="index"
            :class="{'active': isActive(item.path)}">
                <span slot="label">
                    <router-link :to="item.path">{{item.title}}{{isActive(item.path)}}</router-link>
                    <i v-if="isActive(item.path)" class="el-icon-close"  @click="closeTags(index)"></i>
                </span>
            </el-tab-pane>
        </el-tabs> -->
        <ul>
            <li class="tags-li" v-for="(item,index) in tagsList" :class="{'active': isActive(item.path)}" :key="index">
                <router-link :to="item.path" class="tags-li-title">
                    {{item.title}}
                </router-link>
                <span class="tags-li-icon" @click="closeTags(index)"><i v-if="isActive(item.path)" class="el-icon-close"></i></span>
            </li>
        </ul>
        <div class="tags-close-box">
            <el-dropdown @command="handleTags">
                <el-button size="mini">
                    标签选项<i class="el-icon-arrow-down el-icon--right"></i>
                </el-button>
                <el-dropdown-menu size="small" slot="dropdown">
                    <el-dropdown-item command="other">关闭其他</el-dropdown-item>
                    <el-dropdown-item command="all">关闭所有</el-dropdown-item>
                </el-dropdown-menu>
            </el-dropdown>
        </div>
    </div> 
</template>

<script>
   export default {
        data() {
            return {
                tagsList: []
            }
        },
        methods: {
            isActive(path) {
                return path === this.$route.path;
            },
            
            // 设置标签
            setTags(route){
                const isExist = this.tagsList.some(item => {
                    return item.path === route.path;
                })
                !isExist && this.tagsList.push({
                    title: route.meta.title,
                    path: route.path
                })
            },
            // 关闭单个标签
            closeTags(index) {
                const delItem = this.tagsList.splice(index, 1)[0];
                const item = this.tagsList[index] ? this.tagsList[index] : this.tagsList[index - 1];
                if (item) {
                    delItem.path === this.$route.path && this.$router.push(item.path);
                }else{
                    this.$router.push('/');
                }
            },
             // 关闭全部标签
            closeAll(){
                this.tagsList = [];
                this.$router.push('/');
            },
            // 关闭其他标签
            closeOther(){
                const curItem = this.tagsList.filter(item => {
                    return item.path === this.$route.path;
                })
                this.tagsList = curItem;
            },
            handleTags(command){
                command === 'other' ? this.closeOther() : this.closeAll();
            }
           
        },
        computed: {
            showTags() {
                return this.tagsList.length > 0;
            }
        },
        watch:{
            $route(newValue, oldValue){
                this.setTags(newValue);
            },
            
        },
        created(){
            // console.log(this.$route),
            this.setTags(this.$route);
        }
    } 
</script>

<style>
    .tags_wrap {
        height: 40px;
        background-color: #ffffff;
        box-shadow: 0px 0px 10px 0px 
		rgba(0, 0, 0, 0.17);
        /* margin-left: -6px; */
        overflow: hidden;
        /* position: fixed;
        width:100%; */
    }
    .tags_wrap ul{
        margin-top: 0;
        margin-left: -36px;
    }
    .tags-li {
        float: left;
        border-radius: 0px 0px 10px 10px;
        font-size: 14px;
        overflow: hidden;
        cursor: pointer;
        height: 36px;
        line-height: 36px;
        border: 1px solid #e9eaec;
        background: #fff;
        padding: 2px 16px;
        vertical-align: middle;
        color: #666;
        -webkit-transition: all .3s ease-in;
        -moz-transition: all .3s ease-in;
        transition: all .3s ease-in;
    }
    .tags-li a{
        text-decoration: none;
    }
    .tags-li:not(.active):hover {
        background: #f8f8f8;
    }
    .tags-li-title {
        float: left;
        /* margin-right: 5px; */
        color: #666;
    }
    .tags-li.active,.tags-li.active .tags-li-title {
        background-color: #ff5263;
        color: #ffffff;
    }
    .tags-close-box {
        float:right;
        margin-top: -14px;
        box-sizing: border-box;
        text-align: center;
        /* width: 110px; */
        height: 40px;
        padding-top: 5px;
        z-index: 10;
        
    }
    /* .tags_wrap{
        height: 40px;
        background-color: #ffffff;
        overflow: hidden;
        box-shadow: 0px 0px 10px 0px 
		rgba(0, 0, 0, 0.17);
        margin-left: -6px;
    } */
    /* .el-tabs__nav .active{
        background-color: #ff5263;
        color: #ffffff;
    }
    .el-tabs__item a{
        text-decoration: none;
        color: #1a1a1a;
    } */
    /* .el-tabs__nav-scroll{
        background-color: #ffffff;
    }
    .el-tabs__item{
        border-radius: 0px 0px 10px 10px;
    } */
    /* 
    .active{
        background-color: #ff5263;
        color: #ffffff;
    } */
    /* .el-tabs--border-card>.el-tabs__header .el-tabs__item.is-active{
        background-color: #ff5263;
        color: #ffffff;
    } */
    /* .el-tabs__item.is-active a{
        color: #ffffff;
    } */
    /* .el-tabs--border-card>.el-tabs__header .el-tabs__item:not(.is-disabled):hover{
        color: #ffffff;
    } */
    /* .el-icon-close:active{
        color: #ffffff;
    } */
    .el-icon-close{
        position: absolute;
    }
</style>

